﻿package com.snakedickgames.common.sandy 
{
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.PBE;
    import com.pblabs.engine.components.AnimatedComponent;
    import com.pblabs.rendering2D.ui.IUITarget;
    import flash.display.Sprite;
    import sandy.core.Scene3D;
    import sandy.core.scenegraph.Camera3D;
    import sandy.core.scenegraph.Group;
    import sandy.core.scenegraph.Node;
    
    
    public class SandyScene3D extends AnimatedComponent
    {
        public var sceneName:String = "SandyScene";
        public var camera:SandyCamera3D;
        public var rootGroup:Group = new Group("root");
        
        protected var _scene:Scene3D;
        protected var _sceneView:IUITarget;
        protected var _sceneViewName:String = null;
        protected var _rootSprite:Sprite;
        
        public function SandyScene3D() 
        {
            _rootSprite = generateRootSprite();
        }
        
        protected function generateRootSprite():Sprite
        {
            var s:Sprite = new Sprite();
            s.name = "Sandy3DSceneRoot";
            return s;
        }
        
        public function add(child:SandyNode):void
        {
            if (_scene)
            {
                if (_scene.root)
                    _scene.root.addChild(child.node);
            }
        }
        
        public function remove(child:SandyNode):void
        {
            child.node.remove();
        }
                
        public function get sceneView():IUITarget
        {
            if (!_sceneView && _sceneViewName)
                _sceneView = PBE.findChild(_sceneViewName) as IUITarget
                
            return _sceneView;
        }
        
        public function set sceneView(value:IUITarget):void
        {
            if (_sceneView)
            {
                _sceneView.removeDisplayObject(_rootSprite);
            }
            
            _sceneView = value;
            
            if (_sceneView)
            {
                _sceneView.addDisplayObject(_rootSprite);
            }
            Logger.print(this, "Set sceneview");
        }
        
        public function get sceneViewName():String
        {
            return _sceneViewName;
        }
        
        public function set sceneViewName(value:String):void
        {
            _sceneViewName = value;
        }
        
        protected function _createScene():void
        {
            if (!camera || !camera.isRegistered)
                return;            
            _scene = new Scene3D(sceneName, _rootSprite, camera.camera, rootGroup);
            Logger.print(this, "created scene");
        }
        
        override protected function onReset():void 
        {
            super.onReset();
            Logger.print(this, "resetting SCene3D");
            if (!_scene)
                _createScene();
        }
        
        override public function onFrame(deltaTime:Number):void 
        {
            super.onFrame(deltaTime);
            if (_scene)
            {
                _scene.render();
            }
        }
    }

}